package cqg.processor;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * Search xml files to be processed by Cqg.
 * <br><br>
 * @author Julio César Pérez Arques
 */
public class XmlFileSearcher {
	
	/**
	 * Return a list of the xml file paths that contains the indicated folder path
	 * @param folderPath
	 * @return
	 */
	public List<String> search(String folderPath) {
		File folderPathFile = new File(folderPath);
		if (!folderPathFile.isDirectory()) {
			throw new IllegalArgumentException("Folder path invalid: must be a folder");
		}
		
		List<String> xmlFilePaths = new LinkedList<String>();
		String[] subFileNames = folderPathFile.list();
		String subFilePath = null;
		File subFile = null;
		for (int i = 0; i < subFileNames.length; i++) {
			if (subFileNames[i].endsWith(".xml")) {
				subFilePath = folderPathFile + File.separator + subFileNames[i];
				subFile = new File(subFilePath);
				if (!subFile.isFile()) {
					continue;
				}
				xmlFilePaths.add(subFilePath);
			}
		}
		return xmlFilePaths;
	}
}
